package org.simplecrm.repository.customer;

import org.simplecrm.domain.customer.Customer;
import org.slf4j.Logger;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.LinkedList;
import java.util.List;

/**
 * Implementation for JDBC database.
 */
@Repository("customerDAO")
public class CustomerJdbcDAO implements CustomerDAO {

    @org.simplecrm.misc.loginject.Logger
    private Logger LOGGER;

    @PersistenceContext(unitName = "entityManager")
    private EntityManager entityManager;

    @Override
    public List<Customer> getAllCustomers() {

        List<Customer> customerList;

        Query query = entityManager.createNamedQuery("Customer.findAll");
        customerList = query.getResultList();
        if (customerList == null) {
            customerList = new LinkedList<>();
        }

        return customerList;
    }

    public List<Customer> getCustomersPage(int pageNumber, int pageSize) {

        List<Customer> customersPage;

        //TODO: is it a good solution to obtain data on every page request???
        Query query = entityManager.createQuery("From Customer");
        query.setFirstResult((pageNumber - 1) * pageSize);
        query.setMaxResults(pageSize);
        customersPage = query.getResultList();

//        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
//
//        CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
//        countQuery.select(criteriaBuilder.count(countQuery.from(Customer.class)));
//        Long count = entityManager.createQuery(countQuery).getSingleResult();
//
//        CriteriaQuery<Customer> criteriaQuery = criteriaBuilder.createQuery(Customer.class);
//        CriteriaQuery<Customer> select = criteriaQuery.select(criteriaQuery.from(Customer.class));
//
//        TypedQuery<Customer> typedQuery = entityManager.createQuery(select);
//        while (pageNumber < count.intValue()) {
//            typedQuery.setFirstResult(pageNumber - 1);
//            typedQuery.setMaxResults(pageSize);
//            System.out.println("Current page: " + typedQuery.getResultList());
//            pageNumber += pageSize;
//        }

        return customersPage;
    }

    @Override
    public Long getCustomersCount() {

        Query query = entityManager.createNamedQuery("Customer.count");
        return (Long) query.getSingleResult();
    }

    @Override
    public void deleteCustomer(int id) {

        Customer customer = entityManager.find(Customer.class, id);
        entityManager.remove(customer);
    }

    @Override
    public Customer getCustomerById(int id) {
        return entityManager.find(Customer.class, id);
    }

    @Override
    public void addNewCustomer(Customer newCustomer) {
        entityManager.persist(newCustomer);
    }

    @Override
    public void updateCustomer(Customer changedCustomer) {

        entityManager.find(Customer.class, changedCustomer.getId());
        entityManager.merge(changedCustomer);
    }
}